<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 6.3.0">
<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400,400italic,700,700italic%7CZCOOL+QingKe+HuangYou:300,300italic,400,400italic,700,700italic%7CNoto+Sans+Simplified+Chinese:300,300italic,400,400italic,700,700italic%7CJetBrains+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="/lib/@fortawesome/fontawesome-free/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/animate.css/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/@fancyapps/ui/dist/fancybox/fancybox.css" integrity="sha256-RvRHGSuWAxZpXKV9lLDt2e+rZ+btzn48Wp4ueS3NZKs=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"wyatex.online","root":"/","images":"/images","scheme":"Pisces","darkmode":true,"version":"8.18.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":true}}</script><script src="/js/config.js"></script>

    <meta name="description" content="来自神光的《Nest 通关秘籍》">
<meta property="og:type" content="article">
<meta property="og:title" content="NestJS笔记1">
<meta property="og:url" content="https://wyatex.online/NestJS/nestjs%E7%AC%94%E8%AE%B01/index.html">
<meta property="og:site_name" content="Wyatex&#96;s blog">
<meta property="og:description" content="来自神光的《Nest 通关秘籍》">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://wyatex.online/NestJS/nestjs%E7%AC%94%E8%AE%B01/formData.webp">
<meta property="og:image" content="https://wyatex.online/NestJS/nestjs%E7%AC%94%E8%AE%B01/lifecycleEvents.png">
<meta property="article:published_time" content="2023-05-27T08:25:19.000Z">
<meta property="article:modified_time" content="2023-06-02T01:49:04.787Z">
<meta property="article:author" content="Wyatex">
<meta property="article:tag" content="NestJS">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://wyatex.online/NestJS/nestjs%E7%AC%94%E8%AE%B01/formData.webp">


<link rel="canonical" href="https://wyatex.online/NestJS/nestjs%E7%AC%94%E8%AE%B01/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://wyatex.online/NestJS/nestjs%E7%AC%94%E8%AE%B01/","path":"NestJS/nestjs笔记1/","title":"NestJS笔记1"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>NestJS笔记1 | Wyatex`s blog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Wyatex`s blog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">97</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">26</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">155</span></a></li><li class="menu-item menu-item-编程"><a href="/programming/" rel="section"><i class="fa fa-book fa-fw"></i>编程</a></li><li class="menu-item menu-item-蹲坑读物"><a href="/%E5%B9%B2%E8%B4%A7/%E5%BC%80%E5%8F%91%E4%BA%A4%E6%B5%81%E7%BE%A4%E5%88%86%E4%BA%AB%E8%AE%B0%E5%BD%95/" rel="section"><i class="fa fa-book fa-fw"></i>蹲坑读物</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#nest-cli"><span class="nav-number">1.</span> <span class="nav-text">nest cli</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E7%A7%8D%E4%BC%A0%E8%BE%93%E6%95%B0%E6%8D%AE%E7%9A%84%E6%96%B9%E5%BC%8F"><span class="nav-number">2.</span> <span class="nav-text">5 种传输数据的方式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#URL-param"><span class="nav-number">2.1.</span> <span class="nav-text">URL param</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#URL-query"><span class="nav-number">2.2.</span> <span class="nav-text">URL query</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#form-urlencoded"><span class="nav-number">2.3.</span> <span class="nav-text">form urlencoded</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#json"><span class="nav-number">2.4.</span> <span class="nav-text">json</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#form-data"><span class="nav-number">2.5.</span> <span class="nav-text">form data</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Provide-%E6%B3%A8%E5%85%A5%E6%96%B9%E5%BC%8F"><span class="nav-number">3.</span> <span class="nav-text">Provide 注入方式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B3%A8%E5%85%A5%E5%88%AB%E7%9A%84%E4%B8%9C%E8%A5%BF%EF%BC%9F"><span class="nav-number">3.1.</span> <span class="nav-text">注入别的东西？</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A8%A1%E5%9D%97%E5%AF%BC%E5%85%A5%E5%92%8C%E5%85%A8%E5%B1%80%E6%A8%A1%E5%9D%97"><span class="nav-number">4.</span> <span class="nav-text">模块导入和全局模块</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="nav-number">5.</span> <span class="nav-text">生命周期</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%90%84%E7%A7%8D%E8%A3%85%E9%A5%B0%E5%99%A8"><span class="nav-number">6.</span> <span class="nav-text">各种装饰器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Middleware-%E4%B8%AD%E9%97%B4%E4%BB%B6"><span class="nav-number">6.1.</span> <span class="nav-text">Middleware 中间件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Guard-%E5%AE%88%E5%8D%AB"><span class="nav-number">6.2.</span> <span class="nav-text">Guard 守卫</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Interceptor-%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="nav-number">6.3.</span> <span class="nav-text">Interceptor 拦截器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Pipe"><span class="nav-number">6.4.</span> <span class="nav-text">Pipe</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ExceptionFilter"><span class="nav-number">6.5.</span> <span class="nav-text">ExceptionFilter</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Nest-%E8%87%AA%E5%B8%A6%E7%9A%84%E8%A3%85%E9%A5%B0%E5%99%A8"><span class="nav-number">7.</span> <span class="nav-text">Nest 自带的装饰器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E8%A3%85%E9%A5%B0%E5%99%A8"><span class="nav-number">8.</span> <span class="nav-text">自定义装饰器</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Wyatex"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Wyatex</p>
  <div class="site-description" itemprop="description">这里是我用来做笔记的网站哟~</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">155</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">97</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wyatex" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wyatex" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:wyatex@qq.com" title="E-Mail → mailto:wyatex@qq.com" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          链接
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <a href="https://antfu.me/" title="https:&#x2F;&#x2F;antfu.me&#x2F;" rel="noopener" target="_blank">Anthony Fu</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://www.codesky.me/" title="https:&#x2F;&#x2F;www.codesky.me&#x2F;" rel="noopener" target="_blank">CodeSky</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://learn.lianglianglee.com/" title="https:&#x2F;&#x2F;learn.lianglianglee.com&#x2F;" rel="noopener" target="_blank">技术文章摘抄</a>
            </li>
        </ul>
      </div>
    </div>
        <div class="pjax">
        <div class="sidebar-inner sidebar-post-related">
          <div class="animated">
              <div class="links-of-blogroll-title"><i class="fa fa-signs-post fa-fw"></i>
    相关文章
  </div>
  <ul class="popular-posts">
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/Vue/Vue%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E9%A2%9D%E5%A4%96%E7%AF%87%EF%BC%881%EF%BC%89%EF%BC%9AVue-%E7%BB%84%E4%BB%B6%E9%80%9A%E4%BF%A1%E7%9A%84-8-%E7%A7%8D%E6%96%B9%E5%BC%8F/" rel="bookmark">
        <time class="popular-posts-time">2021-08-02</time>
        <br>
      Vue学习笔记额外篇（1）：Vue 组件通信的 8 种方式
      </a>
    </li>
  </ul>

          </div>
        </div>
        </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wyatex.online/NestJS/nestjs%E7%AC%94%E8%AE%B01/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Wyatex">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Wyatex`s blog">
      <meta itemprop="description" content="这里是我用来做笔记的网站哟~">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="NestJS笔记1 | Wyatex`s blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          NestJS笔记1
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-05-27 16:25:19" itemprop="dateCreated datePublished" datetime="2023-05-27T16:25:19+08:00">2023-05-27</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-06-02 09:49:04" itemprop="dateModified" datetime="2023-06-02T09:49:04+08:00">2023-06-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/NestJS/" itemprop="url" rel="index"><span itemprop="name">NestJS</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>来自神光的<a target="_blank" rel="noopener" href="https://juejin.cn/book/7226988578700525605">《Nest 通关秘籍》</a></p>
<span id="more"></span>

<h2 id="nest-cli"><a href="#nest-cli" class="headerlink" title="nest cli"></a>nest cli</h2><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 安装cli</span></span><br><span class="line">npm install -g @nestjs/cli</span><br><span class="line"><span class="comment"># 更新cli</span></span><br><span class="line">npm update -g @nestjs/cli</span><br><span class="line"><span class="comment"># 创建项目</span></span><br><span class="line">nest new 项目名</span><br><span class="line"><span class="comment"># 查看cli项目</span></span><br><span class="line">nest -h</span><br><span class="line"><span class="comment"># 生成一个完整的模块</span></span><br><span class="line">nest generate resource xxx</span><br><span class="line"><span class="comment"># 只生成module、controller... 可以nest new -h查看能生成什么</span></span><br><span class="line">nest generate module/controller/... xxx</span><br><span class="line"><span class="comment"># 启动 --watch 监听js和ts变化、--watchAssets 监听所有资源文件、--config 选择nest cli配置文件</span></span><br><span class="line">nest start</span><br></pre></td></tr></table></figure>

<p>设置允许访问静态资源：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">NestExpressApplication</span> &#125; <span class="keyword">from</span> <span class="string">&quot;@nestjs/platform-express&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; join &#125; <span class="keyword">from</span> <span class="string">&quot;path&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">bootstrap</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">const</span> app = (<span class="keyword">await</span> <span class="title class_">NestFactory</span>.<span class="property">create</span>) &lt; <span class="title class_">NestExpressApplication</span> &gt; <span class="title class_">AppModule</span>;</span><br><span class="line">  app.<span class="title function_">useStaticAssets</span>(<span class="title function_">join</span>(__dirname, <span class="string">&quot;..&quot;</span>, <span class="string">&quot;public&quot;</span>), &#123; <span class="attr">prefix</span>: <span class="string">&quot;/static&quot;</span> &#125;);</span><br><span class="line">  <span class="keyword">await</span> app.<span class="title function_">listen</span>(<span class="number">3000</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">bootstrap</span>();</span><br></pre></td></tr></table></figure>

<h2 id="5-种传输数据的方式"><a href="#5-种传输数据的方式" class="headerlink" title="5 种传输数据的方式"></a>5 种传输数据的方式</h2><h3 id="URL-param"><a href="#URL-param" class="headerlink" title="URL param"></a>URL param</h3><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span>(<span class="string">&quot;api/person&quot;</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">PersonController</span> &#123;</span><br><span class="line">  <span class="meta">@Get</span>(<span class="string">&quot;:id&quot;</span>)</span><br><span class="line">  <span class="title function_">urlParm</span>(<span class="params"><span class="meta">@Param</span>(<span class="string">&quot;id&quot;</span>) id: <span class="built_in">string</span></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">`received: id=<span class="subst">$&#123;id&#125;</span>`</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="URL-query"><a href="#URL-query" class="headerlink" title="URL query"></a>URL query</h3><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span>(<span class="string">&quot;api/person&quot;</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">PersonController</span> &#123;</span><br><span class="line">  <span class="meta">@Get</span>(<span class="string">&quot;find&quot;</span>)</span><br><span class="line">  <span class="title function_">query</span>(<span class="params"><span class="meta">@Query</span>(<span class="string">&quot;name&quot;</span>) name: <span class="built_in">string</span>, <span class="meta">@Query</span>(<span class="string">&quot;age&quot;</span>) age: <span class="built_in">number</span></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">`received: name=<span class="subst">$&#123;name&#125;</span>,age=<span class="subst">$&#123;age&#125;</span>`</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="form-urlencoded"><a href="#form-urlencoded" class="headerlink" title="form urlencoded"></a>form urlencoded</h3><p>form urlencoded 是通过 body 传输数据，其实是把 query 字符串放在了 body 里，所以需要做 url encode：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">CreatePersonDto</span> &#125; <span class="keyword">from</span> <span class="string">&quot;./dto/create-person.dto&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">CreatePersonDto</span> &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="built_in">string</span>;</span><br><span class="line">  <span class="attr">age</span>: <span class="built_in">number</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span>(<span class="string">&quot;api/person&quot;</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">PersonController</span> &#123;</span><br><span class="line">  <span class="meta">@Post</span>()</span><br><span class="line">  <span class="title function_">body</span>(<span class="params"><span class="meta">@Body</span>() createPersonDto: CreatePersonDto</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">`received: <span class="subst">$&#123;<span class="built_in">JSON</span>.stringify(createPersonDto)&#125;</span>`</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="json"><a href="#json" class="headerlink" title="json"></a>json</h3><p>代码和上面一样，也是通过 Body 装饰器接收。nest 根据 Content-Type 进行区分，使用不同的方式进行解析</p>
<h3 id="form-data"><a href="#form-data" class="headerlink" title="form data"></a>form data</h3><p>form data 报文有点不一样</p>
<img data-src="/NestJS/nestjs%E7%AC%94%E8%AE%B01/formData.webp" class="">

<p>Nest 解析 form data 使用 FilesInterceptor 的拦截器，用 @UseInterceptors 装饰器启用，然后通过 @UploadedFiles 来取。非文件的内容，同样是通过 @Body 来取。</p>
<p>首先安装一个 ts 依赖</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm i -D @types/multer</span><br><span class="line">pnpm add -D @types/multer</span><br></pre></td></tr></table></figure>

<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">AnyFilesInterceptor</span> &#125; <span class="keyword">from</span> <span class="string">&quot;@nestjs/platform-express&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">CreatePersonDto</span> &#125; <span class="keyword">from</span> <span class="string">&quot;./dto/create-person.dto&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span>(<span class="string">&quot;api/person&quot;</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">PersonController</span> &#123;</span><br><span class="line">  <span class="meta">@Post</span>(<span class="string">&quot;file&quot;</span>)</span><br><span class="line">  <span class="meta">@UseInterceptors</span>(<span class="title class_">AnyFilesInterceptor</span>())</span><br><span class="line">  <span class="title function_">body2</span>(<span class="params"></span></span><br><span class="line"><span class="params">    <span class="meta">@Body</span>() createPersonDto: CreatePersonDto,</span></span><br><span class="line"><span class="params">    <span class="meta">@UploadedFiles</span>() files: <span class="built_in">Array</span>&lt;Express.Multer.File&gt;</span></span><br><span class="line"><span class="params">  </span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(files);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">`received: <span class="subst">$&#123;<span class="built_in">JSON</span>.stringify(createPersonDto)&#125;</span>`</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Provide-注入方式"><a href="#Provide-注入方式" class="headerlink" title="Provide 注入方式"></a>Provide 注入方式</h2><p>先创建个测试的 service：</p>
<figure class="highlight ts"><figcaption><span>testService.ts</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">Injectable</span> &#125; <span class="keyword">from</span> <span class="string">&quot;@nestjs/common&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Injectable</span>()</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">TestService</span> &#123;</span><br><span class="line">  <span class="title function_">getHello</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;HelloWorld!&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在 module 定义一下：</p>
<figure class="highlight ts"><figcaption><span>aaa.Module.ts</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// import...</span></span><br><span class="line"><span class="meta">@Module</span>(&#123;</span><br><span class="line">  <span class="attr">controllers</span>: [<span class="title class_">AaaController</span>],</span><br><span class="line">  <span class="attr">providers</span>: [<span class="title class_">AaaService</span>, <span class="title class_">TestService</span>],</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AaaModule</span> &#123;&#125;</span><br></pre></td></tr></table></figure>

<p>这个 provider 是简写方式，完整写法是这样：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Module</span>(&#123;</span><br><span class="line">  <span class="attr">controllers</span>: [<span class="title class_">AaaController</span>],</span><br><span class="line">  <span class="attr">providers</span>: [</span><br><span class="line">    <span class="title class_">AaaService</span>,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">provide</span>: <span class="title class_">TestService</span>,</span><br><span class="line">      <span class="attr">useClass</span>: <span class="title class_">TestService</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">  ],</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AaaModule</span> &#123;&#125;</span><br></pre></td></tr></table></figure>

<p>也可以自定义一下 provide 的名字：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Module</span>(&#123;</span><br><span class="line">  <span class="attr">controllers</span>: [<span class="title class_">AaaController</span>],</span><br><span class="line">  <span class="attr">providers</span>: [</span><br><span class="line">    <span class="title class_">AaaService</span>,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">provide</span>: <span class="string">&quot;test_service&quot;</span>,</span><br><span class="line">      <span class="attr">useClass</span>: <span class="title class_">TestService</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">  ],</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AaaModule</span> &#123;&#125;</span><br></pre></td></tr></table></figure>

<p>这样 inject 就需要写上 inject 名字：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span>(<span class="string">&quot;aaa&quot;</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AaaController</span> &#123;</span><br><span class="line">  <span class="title function_">constructor</span>(<span class="params"></span></span><br><span class="line"><span class="params">    <span class="keyword">private</span> <span class="keyword">readonly</span> aaaService: AaaService,</span></span><br><span class="line"><span class="params">    <span class="meta">@Inject</span>(<span class="string">&quot;test_service&quot;</span>) <span class="keyword">private</span> <span class="keyword">readonly</span> testService: TestService</span></span><br><span class="line"><span class="params">  </span>) &#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>除了构造器注入，也可以属性注入：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span>(<span class="string">&quot;aaa&quot;</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AaaController</span> &#123;</span><br><span class="line">  <span class="comment">// 如果providers不自动provide，只需要@Inject(TestService)</span></span><br><span class="line">  <span class="meta">@Inject</span>(<span class="string">&quot;test_service&quot;</span>)</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">readonly</span> <span class="attr">testService</span>: <span class="title class_">TestService</span>;</span><br><span class="line"></span><br><span class="line">  <span class="title function_">constructor</span>(<span class="params"><span class="keyword">private</span> <span class="keyword">readonly</span> aaaService: AaaService</span>) &#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="注入别的东西？"><a href="#注入别的东西？" class="headerlink" title="注入别的东西？"></a>注入别的东西？</h3><p>除了 class，也可以注入一些值：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Module</span>(&#123;</span><br><span class="line">  <span class="attr">controllers</span>: [<span class="title class_">AaaController</span>],</span><br><span class="line">  <span class="attr">providers</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">provide</span>: <span class="string">&quot;str&quot;</span>,</span><br><span class="line">      <span class="attr">useValue</span>: <span class="string">&quot;this is string&quot;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">provide</span>: <span class="string">&quot;obj&quot;</span>,</span><br><span class="line">      <span class="attr">useValue</span>: &#123;</span><br><span class="line">        <span class="attr">name</span>: <span class="string">&quot;lin&quot;</span>,</span><br><span class="line">        <span class="attr">age</span>: <span class="number">24</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">provide</span>: <span class="string">&quot;factory&quot;</span>,</span><br><span class="line">      <span class="title function_">useFactory</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">          <span class="attr">name</span>: <span class="string">&quot;lin&quot;</span>,</span><br><span class="line">          <span class="attr">age</span>: <span class="number">24</span>,</span><br><span class="line">        &#125;;</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">provide</span>: <span class="string">&quot;paramFactory&quot;</span>,</span><br><span class="line">      <span class="title function_">useFactory</span>(<span class="params">f: &#123; age: <span class="built_in">number</span> &#125;, str: <span class="built_in">string</span></span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">          <span class="attr">name</span>: str,</span><br><span class="line">          <span class="attr">age</span>: f.<span class="property">age</span>,</span><br><span class="line">        &#125;;</span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="attr">inject</span>: [<span class="string">&quot;factory&quot;</span>, <span class="string">&quot;str&quot;</span>],</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">provide</span>: <span class="string">&quot;asyncFactory&quot;</span>,</span><br><span class="line">      <span class="keyword">async</span> <span class="title function_">useFactory</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">await</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">          <span class="built_in">setTimeout</span>(res, <span class="number">3000</span>);</span><br><span class="line">        &#125;);</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">          <span class="attr">name</span>: <span class="string">&quot;lin&quot;</span>,</span><br><span class="line">          <span class="attr">age</span>: <span class="number">24</span>,</span><br><span class="line">        &#125;;</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">provide</span>: <span class="string">&quot;alias&quot;</span>,</span><br><span class="line">      <span class="attr">useExisting</span>: <span class="string">&quot;str&quot;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">  ],</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AaaModule</span> &#123;&#125;</span><br></pre></td></tr></table></figure>

<p>注入：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span>(<span class="string">&quot;aaa&quot;</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AaaController</span> &#123;</span><br><span class="line">  <span class="meta">@Inject</span>(<span class="title class_">TestService</span>)</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">readonly</span> <span class="attr">testService</span>: <span class="title class_">TestService</span>;</span><br><span class="line">  <span class="meta">@Inject</span>(<span class="string">&quot;str&quot;</span>)</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">readonly</span> <span class="attr">str</span>: <span class="built_in">string</span>;</span><br><span class="line">  <span class="meta">@Inject</span>(<span class="string">&quot;obj&quot;</span>)</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">readonly</span> <span class="attr">obj</span>: &#123; <span class="attr">age</span>: <span class="built_in">number</span>; <span class="attr">name</span>: <span class="built_in">string</span> &#125;;</span><br><span class="line">  <span class="meta">@Inject</span>(<span class="string">&quot;factory&quot;</span>)</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">readonly</span> <span class="attr">factory</span>: &#123; <span class="attr">age</span>: <span class="built_in">number</span>; <span class="attr">name</span>: <span class="built_in">string</span> &#125;;</span><br><span class="line">  <span class="meta">@Inject</span>(<span class="string">&quot;paramFactory&quot;</span>)</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">readonly</span> <span class="attr">paramFactory</span>: &#123; <span class="attr">age</span>: <span class="built_in">number</span>; <span class="attr">name</span>: <span class="built_in">string</span> &#125;;</span><br><span class="line">  <span class="meta">@Inject</span>(<span class="string">&quot;asyncFactory&quot;</span>)</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">readonly</span> <span class="attr">asyncFactory</span>: &#123; <span class="attr">age</span>: <span class="built_in">number</span>; <span class="attr">name</span>: <span class="built_in">string</span> &#125;;</span><br><span class="line">  <span class="meta">@Inject</span>(<span class="string">&quot;alias&quot;</span>)</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">readonly</span> <span class="attr">alias</span>: <span class="built_in">string</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果是用 class 作为 provide，可以不需要显式声明注入类型，但是如果用属性注入需要加上@Inject 否则不会注入：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span>(<span class="string">&quot;aaa&quot;</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AaaController</span> &#123;</span><br><span class="line">  <span class="meta">@Inject</span>()</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">readonly</span> <span class="attr">testService</span>: <span class="title class_">TestService</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="模块导入和全局模块"><a href="#模块导入和全局模块" class="headerlink" title="模块导入和全局模块"></a>模块导入和全局模块</h2><p>如果 aaaModule 需要使用 bbbModule 里导出的一些内容，可以这样写：</p>
<figure class="highlight ts"><figcaption><span>bbb.module.ts</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Module</span>(&#123;</span><br><span class="line">  <span class="attr">controllers</span>: [<span class="title class_">BbbController</span>],</span><br><span class="line">  <span class="attr">providers</span>: [<span class="title class_">BbbProvider</span>],</span><br><span class="line">  <span class="attr">exports</span>: [<span class="title class_">BbbProvider</span>],</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">BbbModule</span> &#123;&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight ts"><figcaption><span>aaa.module.ts</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Module</span>(&#123;</span><br><span class="line">  <span class="attr">imports</span>: [<span class="title class_">BbbModule</span>]</span><br><span class="line">  <span class="attr">controllers</span>: [<span class="title class_">AaaController</span>],</span><br><span class="line">  <span class="attr">providers</span>: [<span class="title class_">AaaProvider</span>],</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AaaModule</span> &#123;&#125;</span><br></pre></td></tr></table></figure>

<p>这样就可以在 aaa 的 controller 或者 service 内注入 BbbService 了</p>
<p>如果不想要每个都导入，可以声明 Bbb 成为全局模块：</p>
<figure class="highlight ts"><figcaption><span>bbb.module.ts</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Global</span>()</span><br><span class="line"><span class="meta">@Module</span>(&#123;</span><br><span class="line">  <span class="attr">controllers</span>: [<span class="title class_">BbbController</span>],</span><br><span class="line">  <span class="attr">providers</span>: [<span class="title class_">BbbProvider</span>],</span><br><span class="line">  <span class="attr">exports</span>: [<span class="title class_">BbbProvider</span>],</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">BbbModule</span> &#123;&#125;</span><br></pre></td></tr></table></figure>

<p>aaaModule 和其他的 module 就不需要写 imports，也能注入 bbbService 了，不过能不用就不用全局注入</p>
<h2 id="生命周期"><a href="#生命周期" class="headerlink" title="生命周期"></a>生命周期</h2><p>官方文档上对 nestjs 应用启动和退出的生命周期说明：</p>
<img data-src="/NestJS/nestjs%E7%AC%94%E8%AE%B01/lifecycleEvents.png" class="">

<p>nest 提供了几个接口：OnModuleInit,OnModuleDestroy,OnApplicationBootstrap,BeforeApplicationShutdown,OnApplicationShutdown</p>
<p>可以在 controller 或者 service 里面实现这些接口</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span>()</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AppController</span></span><br><span class="line">  <span class="keyword">implements</span></span><br><span class="line">    <span class="title class_">OnModuleInit</span>,</span><br><span class="line">    <span class="title class_">OnModuleDestroy</span>,</span><br><span class="line">    <span class="title class_">OnApplicationBootstrap</span>,</span><br><span class="line">    <span class="title class_">BeforeApplicationShutdown</span>,</span><br><span class="line">    <span class="title class_">OnApplicationShutdown</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="title function_">constructor</span>(<span class="params"><span class="keyword">private</span> <span class="keyword">readonly</span> appService: AppService</span>) &#123;&#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Get</span>()</span><br><span class="line">  <span class="title function_">getHello</span>(): <span class="built_in">string</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">appService</span>.<span class="title function_">getHello</span>();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="title function_">onApplicationBootstrap</span>(): <span class="built_in">any</span> &#123;&#125;</span><br><span class="line"></span><br><span class="line">  <span class="title function_">onModuleDestroy</span>(): <span class="built_in">any</span> &#123;&#125;</span><br><span class="line"></span><br><span class="line">  <span class="title function_">onModuleInit</span>(): <span class="built_in">any</span> &#123;&#125;</span><br><span class="line"></span><br><span class="line">  <span class="title function_">onApplicationShutdown</span>(signal?: <span class="built_in">string</span>): <span class="built_in">any</span> &#123;&#125;</span><br><span class="line"></span><br><span class="line">  <span class="title function_">beforeApplicationShutdown</span>(signal?: <span class="built_in">string</span>): <span class="built_in">any</span> &#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>onApplicationShutdown 和 beforeApplicationShutdown 能接收到系统传过来的 signal 系统信号。</p>
<p>所有的生命周期函数都支持 async。</p>
<h2 id="各种装饰器"><a href="#各种装饰器" class="headerlink" title="各种装饰器"></a>各种装饰器</h2><h3 id="Middleware-中间件"><a href="#Middleware-中间件" class="headerlink" title="Middleware 中间件"></a>Middleware 中间件</h3><p>文档还没细说，后面再补</p>
<h3 id="Guard-守卫"><a href="#Guard-守卫" class="headerlink" title="Guard 守卫"></a>Guard 守卫</h3><p>路由守卫作用是在进入某个 controller 前判断使用有权限访问：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Injectable</span>()</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">TestGuard</span> <span class="keyword">implements</span> <span class="title class_">CanActivate</span> &#123;</span><br><span class="line">  <span class="title function_">canActivate</span>(</span><br><span class="line">    <span class="attr">context</span>: <span class="title class_">ExecutionContext</span></span><br><span class="line">  ): <span class="built_in">boolean</span> | <span class="title class_">Promise</span>&lt;<span class="built_in">boolean</span>&gt; | <span class="title class_">Observable</span>&lt;<span class="built_in">boolean</span>&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="title function_">validAuth</span>(context.<span class="title function_">switchToHttp</span>().<span class="title function_">getRequest</span>().<span class="property">headers</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>比如可以检查 headers 里面是否带了 token，token 是否有效。</p>
<p>接着让一个 controller 使用这个 guard：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span>(<span class="string">&quot;aaa&quot;</span>)</span><br><span class="line"><span class="meta">@UseGuards</span>(<span class="title class_">TestGuard</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AaaController</span> &#123;&#125;</span><br></pre></td></tr></table></figure>

<p>也可以全局使用：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> app = <span class="keyword">await</span> <span class="title class_">NestFactory</span>.<span class="title function_">create</span>(<span class="title class_">AppModule</span>);</span><br><span class="line">app.<span class="title function_">useGlobalGuards</span>(<span class="keyword">new</span> <span class="title class_">TestGuard</span>());</span><br><span class="line"><span class="keyword">await</span> app.<span class="title function_">listen</span>(<span class="number">3000</span>);</span><br></pre></td></tr></table></figure>

<p>注意要 new。</p>
<h3 id="Interceptor-拦截器"><a href="#Interceptor-拦截器" class="headerlink" title="Interceptor 拦截器"></a>Interceptor 拦截器</h3><p>Interceptor 是拦截器的意思，可以在目标 Controller 方法前后加入一些逻辑：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Injectable</span>()</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">LoggingInterceptor</span> <span class="keyword">implements</span> <span class="title class_">NestInterceptor</span> &#123;</span><br><span class="line">  <span class="title function_">intercept</span>(<span class="attr">context</span>: <span class="title class_">ExecutionContext</span>，<span class="attr">next</span>: <span class="title class_">CallHandler</span>): <span class="title class_">Observable</span>&lt;<span class="built_in">any</span>&gt; &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;Before...&#x27;</span>)</span><br><span class="line">    <span class="keyword">const</span> now = <span class="title class_">Date</span>.<span class="title function_">now</span>()</span><br><span class="line">    <span class="keyword">return</span> next.<span class="title function_">handle</span>().<span class="title function_">pipe</span>(</span><br><span class="line">      <span class="title function_">tap</span>(<span class="function">() =&gt;</span> <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`After... <span class="subst">$&#123;<span class="built_in">Date</span>.now() - now&#125;</span>ms`</span>))</span><br><span class="line">      )</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Interceptor 支持每个路由单独启用，只作用于某个 controller，也同样支持全局启用，作用于全部 controller：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 单个controller</span></span><br><span class="line"><span class="meta">@UseInterceptors</span>(<span class="keyword">new</span> <span class="title class_">LoggingInterceptor</span>())</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">CatsController</span> &#123;&#125;</span><br><span class="line"><span class="comment">// 整个app</span></span><br><span class="line"><span class="keyword">const</span> app = <span class="keyword">await</span> <span class="title class_">NestFactory</span>.<span class="title function_">create</span>(<span class="title class_">ApplicationModule</span>);</span><br><span class="line">app.<span class="title function_">useGlobalInterceptors</span>(<span class="keyword">new</span> <span class="title class_">LoggingInterceptor</span>());</span><br></pre></td></tr></table></figure>

<h3 id="Pipe"><a href="#Pipe" class="headerlink" title="Pipe"></a>Pipe</h3><p>Pipe 是管道的意思，用来对参数做一些检验和转换：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Injectable</span>()</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">ValidationPipe</span> <span class="keyword">implements</span> <span class="title class_">PipeTransform</span> &#123;</span><br><span class="line">  <span class="title function_">transform</span>(<span class="params">value: <span class="built_in">any</span>， metadata: ArgumentMetadata</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> value</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Pipe 要实现 PipeTransform 接口，实现 transform 方法，里面可以对传入的参数值 value 做参数验证，比如格式、类型是否正确，不正确就抛出异常。也可以做转换，返回转换后的值。</p>
<p>内置的有 9 个 Pipe，从名字就能看出它们的意思：</p>
<ul>
<li>ValidationPipe</li>
<li>ParseIntPipe</li>
<li>ParseBoolPipe</li>
<li>ParseArrayPipe</li>
<li>ParseUUIDPipe</li>
<li>DefaultValuePipe</li>
<li>ParseEnumPipe</li>
<li>ParseFloatPipe</li>
<li>ParseFilePipe</li>
</ul>
<p>同样，Pipe 可以只对某个参数生效，某个路由生效，也可以对每个路由都生效：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span>()</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AppController</span> &#123;</span><br><span class="line">  <span class="meta">@Get</span>()</span><br><span class="line">  <span class="title function_">test</span>(<span class="params"><span class="meta">@Param</span>(<span class="string">&#x27;aaa&#x27;</span>, ParseIntPipe) aaa: <span class="built_in">number</span></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span> + aaa</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Post</span>()</span><br><span class="line">  <span class="meta">@UsePipes</span>(<span class="title class_">ValidationPipe</span>)</span><br><span class="line">  <span class="keyword">async</span> <span class="title function_">create</span>(<span class="params"><span class="meta">@Body</span>() createDto: CreateDto</span>) &#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 整个应用</span></span><br><span class="line"><span class="keyword">const</span> app = <span class="keyword">await</span> <span class="title class_">NestFactory</span>.<span class="title function_">create</span>(<span class="title class_">ApplicationModule</span>);</span><br><span class="line">app.<span class="title function_">useGlobalPipes</span>(<span class="keyword">new</span> <span class="title class_">ValidationPipe</span>());</span><br></pre></td></tr></table></figure>

<h3 id="ExceptionFilter"><a href="#ExceptionFilter" class="headerlink" title="ExceptionFilter"></a>ExceptionFilter</h3><p>ExceptionFilter 可以对抛出的异常做处理，返回对应的响应：</p>
<h2 id="Nest-自带的装饰器"><a href="#Nest-自带的装饰器" class="headerlink" title="Nest 自带的装饰器"></a>Nest 自带的装饰器</h2><ul>
<li>@Module： 声明 Nest 模块</li>
<li>@Controller：声明模块里的 controller（可以传字符串，也可以传对象比如：<code>&#123; host: &#39;:host.0.0.1&#39;, path: &#39;aaa&#39; &#125;</code>，通过<code>@HostParam(&#39;host&#39;)</code>取到 host）</li>
<li>@Injectable：声明模块里可以注入的 provider（这个 provider 不只是 service，也可以是 Guard、Interceptor 等等）</li>
<li>@Inject：通过 token 手动指定注入的 provider，tonken 可以是 class 或者 string</li>
<li>@Optional：声明注入的 provider 是可选的，可以为空</li>
<li>@Catch：声明 exception filter 处理的 exception 类型</li>
<li>@UseFilters：路由级别使用 exception filter</li>
<li>@UsePipes：路由级别使用 pipe</li>
<li>@UseInterceptors：路由级别使用 interceptor</li>
<li>@SetMetadata：在 class 或者 handler 上添加 metadata</li>
<li>@Get、@Post、@Put、@Delete、@Patch、@Options、@Head：声明 get、post、put、- delete、patch、options、head 的请求方式</li>
<li>@Param：取出 url 中的参数，比如 &#x2F;aaa&#x2F;:id 中的 id</li>
<li>@Query: 取出 query 部分的参数，比如 &#x2F;aaa?name&#x3D;xx 中的 name</li>
<li>@Body：取出请求 body，通过 dto class 来接收</li>
<li>@Headers：取出某个或全部请求头</li>
<li>@Session：取出 session 对象，需要启用 express-session 中间件</li>
<li>@HostParm： 取出 host 里的参数</li>
<li>@Req、@Request：注入 request 对象</li>
<li>@Res、@Response：注入 response 对象，一旦注入了这个 Nest 就不会把返回值作为响应了，除非指定 passthrough 为 true</li>
<li>@Next：注入调用下一个 handler 的 next 方法</li>
<li>@HttpCode： 修改响应的状态码</li>
<li>@Header：修改响应头</li>
<li>@Redirect：指定重定向的 url</li>
<li>@Render：指定渲染用的模版引擎</li>
</ul>
<h2 id="自定义装饰器"><a href="#自定义装饰器" class="headerlink" title="自定义装饰器"></a>自定义装饰器</h2><p>直接用 cli 生成模板：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nest g decorator aaa</span><br></pre></td></tr></table></figure>

<p>生成结果</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">SetMetadata</span> &#125; <span class="keyword">from</span> <span class="string">&quot;@nestjs/common&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">Aaa</span> = (<span class="params">...args: <span class="built_in">string</span>[]</span>) =&gt; <span class="title class_">SetMetadata</span>(<span class="string">&quot;aaa&quot;</span>, args);</span><br></pre></td></tr></table></figure>

<p>使用:</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">AaaGuard</span> <span class="keyword">implements</span> <span class="title class_">CanActivate</span> &#123;</span><br><span class="line">  <span class="meta">@Inject</span>(<span class="title class_">Reflector</span>)</span><br><span class="line">  <span class="keyword">private</span> <span class="attr">reflector</span>: <span class="title class_">Reflector</span>;</span><br><span class="line"></span><br><span class="line">  <span class="title function_">canActivate</span>(</span><br><span class="line">    <span class="attr">context</span>: <span class="title class_">ExecutionContext</span></span><br><span class="line">  ): <span class="built_in">boolean</span> | <span class="title class_">Promise</span>&lt;<span class="built_in">boolean</span>&gt; | <span class="title class_">Observable</span>&lt;<span class="built_in">boolean</span>&gt; &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">reflector</span>.<span class="title function_">get</span>(<span class="string">&quot;aaa&quot;</span>, context.<span class="title function_">getClass</span>()));</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span>(<span class="string">&quot;aaa&quot;</span>)</span><br><span class="line"><span class="meta">@UseGuards</span>(<span class="title class_">AaaGuard</span>)</span><br><span class="line"><span class="meta">@Aaa</span>(<span class="string">&quot;321&quot;</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AaaController</span> &#123;</span><br><span class="line">  <span class="comment">// ......</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>当然给 handler 用也行：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">AaaGuard</span> <span class="keyword">implements</span> <span class="title class_">CanActivate</span> &#123;</span><br><span class="line">  <span class="meta">@Inject</span>(<span class="title class_">Reflector</span>)</span><br><span class="line">  <span class="keyword">private</span> <span class="attr">reflector</span>: <span class="title class_">Reflector</span>;</span><br><span class="line"></span><br><span class="line">  <span class="title function_">canActivate</span>(</span><br><span class="line">    <span class="attr">context</span>: <span class="title class_">ExecutionContext</span></span><br><span class="line">  ): <span class="built_in">boolean</span> | <span class="title class_">Promise</span>&lt;<span class="built_in">boolean</span>&gt; | <span class="title class_">Observable</span>&lt;<span class="built_in">boolean</span>&gt; &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">reflector</span>.<span class="title function_">get</span>(<span class="string">&quot;aaa&quot;</span>, context.<span class="title function_">getHandler</span>()));</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span>(<span class="string">&quot;aaa&quot;</span>)</span><br><span class="line"><span class="meta">@UseGuards</span>(<span class="title class_">AaaGuard</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">AaaController</span> &#123;</span><br><span class="line">  <span class="meta">@Get</span>()</span><br><span class="line">  <span class="meta">@Aaa</span>(<span class="string">&quot;321&quot;</span>)</span><br><span class="line">  <span class="title function_">test</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Wyatex
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://wyatex.online/NestJS/nestjs%E7%AC%94%E8%AE%B01/" title="NestJS笔记1">https://wyatex.online/NestJS/nestjs笔记1/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/NestJS/" rel="tag"># NestJS</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/JavaScript/%E4%BC%98%E9%9B%85%E7%9A%84%E5%B9%B2%E6%8E%89%E4%B8%8D%E5%BA%94%E8%AF%A5%E7%94%A8%E7%9A%84if-else%EF%BC%88JS%E7%89%88%EF%BC%89/" rel="prev" title="优雅的干掉不应该使用的if-else（JS版）">
                  <i class="fa fa-angle-left"></i> 优雅的干掉不应该使用的if-else（JS版）
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/TypeORM/typeorm%E7%AC%94%E8%AE%B01/" rel="next" title="typeorm笔记1">
                  typeorm笔记1 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2019 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Wyatex</span>
  </div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/ribbon.js/dist/ribbon.min.js"></script>
  <script src="/lib/animejs/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="/lib/@next-theme/pjax/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="/lib/@fancyapps/ui/dist/fancybox/fancybox.umd.js" integrity="sha256-q8XkJ6dj5VwSvzI8+nATCHHQG+Xv/dAZBCgqmu93zOY=" crossorigin="anonymous"></script>
  <script src="/lib/lozad/dist/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="/lib/hexo-generator-searchdb/dist/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
